package com.heeexy.example.controller.web;

import com.github.pagehelper.Page;
import com.heeexy.example.service.ActiceService;
import com.heeexy.example.service.CommentService;
import org.apache.tomcat.util.net.openssl.ciphers.Authentication;
import org.springframework.web.bind.annotation.*;
import yinwei.bo.common.entry.Comment;
import yinwei.bo.common.entry.actice;
import yinwei.bo.common.result.R;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @Resource
    private ActiceService tBlogService;

    //查询评论列表
    @GetMapping("/comments/{blogId}")
    public List<Comment> comments(@PathVariable int blogId) {
        return commentService.listCommentByBlogId(blogId);
    }

    //查询所有评论
    @GetMapping("/getCommentByPage")
    public Page<Comment> getCommentByPage(Long current, Long size){
        return commentService.getCommentByPage(current, size);
    }

    //根据评论人姓名查询评论
    @GetMapping("/comments")
    public List<Comment> findCommentByNickname(String nickname){
        return commentService.findCommentByNickname(nickname);
    }


    //新增评论
    @PostMapping("/comments")
    public List<Comment> post(@RequestBody Comment comment, Authentication authentication, HttpServletRequest request) {
        //获取评论人的ip
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        int blogId = comment.getBlogId();
        if (authentication != null){
           /* TUser user = (TUser)authentication.getPrincipal();
            tComment.setAvatar(user.getAvatar());
            tComment.setAdminComment(true);
            tComment.setIp(ip);*/
        }
        else {
            //设置头像
            comment.setAvatar("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2070453827,1163403148&fm=26&gp=0.jpg");
            comment.setIp(ip);
        }
        if (comment.getParentComment() != null) {
            comment.setParentCommentId(comment.getParentComment().getId());
        }
        commentService.saveComment(comment);
        actice tBlog = tBlogService.findActiceById(blogId);
        tBlog.setCommentCount(tBlog.getCommentCount() + 1);
        tBlogService.updateByComment(tBlog);
        return commentService.listCommentByBlogId(blogId);
    }

    //删除评论
    @DeleteMapping("/deleteComment")
    public R delete(Long id){
        commentService.deleteComment(id);
        return R.ok();
    }

}
